<?php

class Admin_Model_Brand {

    private $_dbtable;

    public function __construct() {
        $this->_dbtable = new Admin_Model_DbTable_Brand();
    }

    /**
     * 
     * @param type $enabled 
     * null => get all
     * true => only enabled
     * false => only disabled
     * @return type
     */
    public function fetchAll($enabled = "full") {
        $select = $this->_dbtable->select();
        switch ($enabled) {
            case "true":
                $select->where("enabled = true");
                break;
            case "false":
                $select->where("enabled = false");
                break;
            case "full":
            default:
                break;
        }
        return $this->_dbtable->fetchAll($select);
    }

    /**
     * 
     * @param type $data
     * @return type
     */
    public function addNewBrand($data) {
        return $this->_dbtable->insert($data);
    }

    /**
     * 
     * @param type $data
     * @param type $id
     * @return type
     */
    public function updateBrand($data, $id) {
        return $this->_dbtable->update($data, "id = $id");
    }

    /**
     * 
     * @param type $id
     * @return type
     */
    public function getBrandById($id, $formated = false) {
        $select = $this->_dbtable->select();
        $select->where("id = $id");
        $result = $this->_dbtable->fetchAll($select);
        if (!$formated) {
            return $result;
        } else {
            return $this->getBrandInArrayFormat($result[0]);
        }
    }

    public function getBrandInArrayFormat($data) {
        #get menu
        $menuDb = new Admin_Model_Menu();
        $menuById = $menuDb->getMenuById($data['menu_id']);
        #format into array
        $brandArray = array();
        $brandArray['brand_id'] = $data['id'];
        $brandArray['brand_name'] = $data['name'];
        $brandArray['brand_image'] = $data['image'];
        $brandArray['menu'] = array(
            'menu_id' => $menuById[0]['id'],
            'menu_name' => $menuById[0]['name'],
        );
        $brandArray['enabled'] = $data['enabled'];
        return $brandArray;
    }

    /**
     * 
     * @param type $enabled
     * @param type $formated
     * @return array
     */
    public function getAllBrands($enabled = "true", $formated = false) {
        $select = $this->_dbtable->select();
        switch ($enabled) {
            case "true":
                $select->where("enabled = true");
                break;
            case "false":
                $select->where("enabled = false");
                break;
            case "full":
            default:
                break;
        }
        $result = $this->_dbtable->fetchAll($select);
        if ($formated) {
            $formatedArray = array();
            foreach ($result as $brandValue) {
                array_push($formatedArray, $this->getBrandInArrayFormat($brandValue));
            }
            return $formatedArray;
        } else {
            return $result;
        }
    }

    /**
     * 
     * @param type $menuId
     * @param type $enabled 1) true, 2)false, 3)null
     * Default = true
     * @param type $formated
     * @return type
     */
    public function getBrandsByMenu($menuId, $enabled = "true", $formated = false) {
        $select = $this->_dbtable->select();
        $select->where("menu_id = ?", $menuId);
        switch ($enabled) {
            case "true":
                $select->where("enabled = true");
                break;
            case "false":
                $select->where("enabled = false");
                break;
            case "full":
            default:
                break;
        }
        $result = $this->_dbtable->fetchAll($select);
        if ($formated) {
            $formatedArray = array();
            foreach ($result as $brandValue) {
                array_push($formatedArray, $this->getBrandInArrayFormat($brandValue));
            }
            return $formatedArray;
        } else {
            return $result;
        }
    }

    /**
     * 
     * @param type $menuId
     * @return array
     */
    public function getBrandsWithStateId() {
        $brandStateArray = array();
        //$brandsByMenu = $this->getBrandsByMenu($menuId);
        $storeDb = new Admin_Model_Store();
        $fetchAllEnabledStore = $storeDb->fetchAll(true, true);
        foreach ($fetchAllEnabledStore as $store) {
            $brandsArray = $store['brands'];
            $state = $store['location']['state']['id'];
            foreach ($brandsArray as $brand) {
                $localArray = array(
                    'brand_id' => $brand['brand_id'],
                    'state_id' => $state
                );
                if (!$this->isMatchFounded($localArray, $brandStateArray)) {
                    array_push($brandStateArray, $localArray);
                }
            }
        }
        return $brandStateArray;
    }

    private function isMatchFounded($localArray, $mainArray) {
        foreach ($mainArray as $array) {
            if ($array['state_id'] == $localArray['state_id'] && $array['brand_id'] == $localArray['brand_id']) {
                return true;
            }
        }
        return false;
    }

}